<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="尺码名称" class="myform-item">
          <el-input
            v-model="form.title"
            placeholder="请输入尺码名称"
            clearable
            @keyup.enter.native="onSearch"
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 50px">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px">
          <el-button
            type="primary"
            @click="dialogVisible = true"
            icon="el-icon-plus"
            >新建</el-button
          >
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="尺码名称" align="center" prop="s_title" fixed>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <div
            :style="{ color: row.status == 1 ? '#67C23A' : '#F56C6C' }"
          >
            {{ row.status == 1 ? "启用" : "禁用" }}
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" fixed="right">
        <template slot-scope="{ row }">
          <el-button type="text" @click="onEdit(row)">编辑</el-button>
          <el-button
            type="text"
            style="color: red"
            @click="onIs(row.id, 0)"
            >删除</el-button
          >
          <!-- <el-button
            type="text"
            style="color: #67c23a"
            v-if="row.status == 0"
            @click="onIs(row.id, 1)"
            >启用</el-button
          > -->
          <!-- <el-button type="text" style="color: red" @click="onDelete(row.id)"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminOperateGetSize"
    />
    <el-dialog
      title="尺码名称"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        <el-form ref="form" :model="data" :rules="rules" label-width="100px">
          <el-form-item
            label="尺码名称"
            class="myform-item"
            prop="s_title"
          >
            <el-input
              style="width: 300px"
              v-model="data.s_title"
              placeholder="请输入尺码名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm('form')"
          :loading="loadingSub"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminOperateGetSize,
  postAdminOperateDisableSize,
  postAdminOperateSetSize,
  postAdminOperateDelSize
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      loadingSub: false,
      dialogVisible: false, //审核
      form: {
        title: "",
        type: 1,
        page: 1,
        size: 10,
      },
      tableData: [],
      total: 1,
      data: {
        s_title: "",
        type: 1,
      },
      rules: {
        s_title: [
          { required: true, message: "请输入部位名称", trigger: "blur" },
        ],
      },
    };
  },
  activated() {
    let form = this.form;
    this.postAdminOperateGetSize(form);
  },
  methods: {
    handleClose() {
      this.data = {
        s_title: "",
        type: 1,
      };
      this.dialogVisible = false;
    },
    // 查看
    onEdit(row) {
      this.data = {
        id: row.id,
        s_title: row.s_title,
        type: 1,
      };
      this.dialogVisible = true;
    },
    async submitForm(formName) {
      this.loadingSub = true;
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            let res = await postAdminOperateSetSize(this.data);
            let form = this.form;
            this.postAdminOperateGetSize(form);
            this.handleClose();
            this.loadingSub = false;
          } catch (error) {
            this.loadingSub = false;
          }
        } else {
          console.log("error submit!!");
          this.loadingSub = false;
          return false;
        }
      });
    },
    async onIs(id, status) {
      let obj = {
        id: id,
        is_disable: status,
      };
      this.$confirm(`确定删除？`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await postAdminOperateDisableSize(obj);
          let form = this.form;
          this.postAdminOperateGetSize(form);
        })
        .catch(() => {});
    },
    // 删除
    onDelete(id) {
      let obj = {
        id,
      };
      let { form } = this;
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(111111111);
          postAdminOperateDelSize(obj).then((res) => {
            this.postAdminOperateGetSize(form);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    // 采购列表
    async postAdminOperateGetSize(data) {
      this.loading = true;
      this.form = data;
      postAdminOperateGetSize(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminOperateGetSize(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
